package cn.school.teacher.evaluation.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.school.common.utils.SysUserUtils;
import cn.school.teacher.evaluation.dao.TeacherEvaluationSchemeDao;
import cn.school.teacher.evaluation.entity.TeacherEvaluationScheme;
import cn.school.teacher.evaluation.service.EvaluationSchemeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Map;

@Slf4j
@Service("evaluationSchemeService")
public class EvaluationSchemeServiceImpl extends ServiceImpl<TeacherEvaluationSchemeDao, TeacherEvaluationScheme> implements EvaluationSchemeService {

    @Override
    public IPage<TeacherEvaluationScheme> getPage(Map<String, Object> params) {
        int pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
        int pageNum = Integer.parseInt(String.valueOf(params.get("pageNum")));

        String beginTime = (String) params.get("beginTime");
        String endTime = (String) params.get("endTime");

        LambdaQueryWrapper<TeacherEvaluationScheme> wrapper = new LambdaQueryWrapper<>();


        if (StrUtil.isNotEmpty(beginTime)) {
            wrapper.gt(TeacherEvaluationScheme::getCreateTime, beginTime + " 00:00:00");
        }

        if (StrUtil.isNotEmpty(endTime)) {
            wrapper.lt(TeacherEvaluationScheme::getCreateTime, endTime + "23:59:59");
        }

        //租户
        wrapper.eq(TeacherEvaluationScheme::getTenantId, SysUserUtils.getSysUser().getTenantId());
        wrapper.orderByDesc(TeacherEvaluationScheme::getCreateTime);

        //wrapper.apply(params.get(CommonConstant.SQL_FILTER) != null, (String) params.get(CommonConstant.SQL_FILTER));
        return page(new Page<>(pageNum, pageSize), wrapper);
    }

    @Override
    public TeacherEvaluationScheme getById(Long id) {
        TeacherEvaluationScheme scheme = this.baseMapper.selectById(id);
        return scheme;
    }

    @Override
    public int update(Long id, TeacherEvaluationScheme scheme) {
        scheme.setId(id);
        int count = this.baseMapper.updateById(scheme);
        return count;
    }

    @Override
    public int create(TeacherEvaluationScheme scheme) {
        int count = this.baseMapper.insert(scheme);
        return count;
    }

    @Override
    public int deleteByIds(Long[] ids) {
        int count;
        count = this.baseMapper.deleteBatchIds(Arrays.asList(ids));
        return count;
    }
}
